package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.zhenhuan.secondProcess.UserZhenHuanSecondProcessData;
import com.yanqu.road.entity.activity.zhenhuan.thirdProcess.UserZhenHuanThirdProcessData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class UserZhenHuanThirdProcessDaoImpl extends TempDataObjectDao<UserZhenHuanThirdProcessData> {
    public static String getUserZhenHuanThirdProcessDataSql = "select * from `t_u_zhenhuan_third` where `activityId`=? and `userId`=?";
    public UserZhenHuanThirdProcessData getUserZhenHuanThirdProcessData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserZhenHuanThirdProcessDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserZhenHuanThirdProcessDataSql = "update `t_u_zhenhuan_third` set lastJoinTime=?,isWorship=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(UserZhenHuanThirdProcessData userZhenHuanThirdProcessData) {
        userZhenHuanThirdProcessData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanThirdProcessData.getLastJoinTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userZhenHuanThirdProcessData.isWorship()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanThirdProcessData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanThirdProcessData.getUserId()));
        if (getDb().executeNoneQuery(updateUserZhenHuanThirdProcessDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserZhenHuanThirdProcessDataSql = "insert into `t_u_zhenhuan_third`(activityId,userId,lastJoinTime,isWorship) values (?,?,?,?)";
    @Override
    protected boolean addData(UserZhenHuanThirdProcessData userZhenHuanThirdProcessData) {
        userZhenHuanThirdProcessData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanThirdProcessData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanThirdProcessData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanThirdProcessData.getLastJoinTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userZhenHuanThirdProcessData.isWorship()));
        if (getDb().executeNoneQuery(insertUserZhenHuanThirdProcessDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserZhenHuanThirdProcessData parseData(ResultSet rs) throws SQLException {
        UserZhenHuanThirdProcessData userZhenHuanThirdProcessData = new UserZhenHuanThirdProcessData();
        userZhenHuanThirdProcessData.setActivityId(rs.getInt("activityId"));
        userZhenHuanThirdProcessData.setUserId(rs.getLong("userId"));
        userZhenHuanThirdProcessData.setLastJoinTime(rs.getLong("lastJoinTime"));
        userZhenHuanThirdProcessData.setWorship(rs.getBoolean("isWorship"));

        return userZhenHuanThirdProcessData;
    }
}
